package com.shanzmoo.core.manager.base;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.http.MethodType;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 阿里云短信服务
 *
 * @author： Gzhao 2021/3/22
 */
@Slf4j
@Component
public class AliSmsManager {

    @Value("${aliSms.accessKeyId}")
    private String accessKeyId;

    @Value("${aliSms.accessSecret}")
    private String accessSecret;

    @Value("${aliSms.regionId}")
    private String regionId;

    @Value("${aliSms.sysDomain}")
    private String sysDomain;

    @Value("${aliSms.sysVersion}")
    private String sysVersion;

    @Value("${aliSms.sysAction}")
    private String sysAction;

    @Value("${aliSms.signName}")
    private String signName;

    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 10, 30L,
            TimeUnit.SECONDS, new LinkedBlockingDeque<>(200000));

    private void sendSms(String template, String phone, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction(sysAction);
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", template);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);

        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());

        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("短信发送，返回结果：{}", response.getData());

        } catch (ServerException e) {

            log.error("短信服务端异常", e);
        } catch (ClientException e) {

            log.error("短信客户端异常", e);
        }
    }

    /**
     * 发送注册短信
     *
     * @author Gzhao 2021/3/23
     * @param
     * @return
     */
    public void sendRegisterSms(String phone, String code) {
        threadPoolExecutor.execute(()->{
            sendSms(AliSmsTemplate.REGISTER, phone, code);
        });
    }

    /**
     * 发送登录短信
     *
     * @author Gzhao 2021/3/23
     * @param
     * @return
     */
    public void sendLoginSms(String phone, String code) {
        threadPoolExecutor.execute(()->{
            sendSms(AliSmsTemplate.LOGIN, phone, code);
        });
    }

    /**
     * 阿里云算下模板，需在阿里云后台配置
     *
     * @author： Gzhao 2021/3/23
     */
    private static class AliSmsTemplate {

        private AliSmsTemplate() {}

        /**
         * 模板：用户注册验证码
         */
        public static final String REGISTER = "SMS_208275096";

        /**
         * 模板：登录确认验证码
         */
        public static final String LOGIN = "SMS_208275098";
    }

}

